package cn.joeg.app.web.core.vo;

import com.ecfront.dew.common.JsonHelper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Date;

@ApiModel("Widget请求响应")
public class WidgetDataResp {

    public static final String DS_CACHE = "cache";
    public static final String DS_MAIN = "main";
    public static final String DS_SECOND = "second";

    @ApiModelProperty(value = "数据来源", notes = "正常的来源是[cache][main]，对应的缓存和主数据源，当前两者不可用时可能是[second]，即备选数据源")
    private String ds;
    @ApiModelProperty(value = "数据刷新时间", notes = "当使用备选数据源时此字段可以用于参考数据的时效")
    private Date flushTime;

    @ApiModelProperty(value = "数据结果", notes = "可以是单条/列表/分页结构")
    private JsonNode data;

    public static WidgetDataResp build(String ds, Date flushTime, Object data) {
        WidgetDataResp widgetDataResp = new WidgetDataResp();
        widgetDataResp.ds = ds;
        widgetDataResp.flushTime = flushTime;
        widgetDataResp.data = JsonHelper.toJson(data);
        return widgetDataResp;
    }

    public static WidgetDataResp build(Object data) {
        WidgetDataResp widgetDataResp = new WidgetDataResp();
        widgetDataResp.ds = DS_MAIN;
        widgetDataResp.flushTime = new Date();
        widgetDataResp.data = JsonHelper.toJson(data);
        return widgetDataResp;
    }

    public static WidgetDataResp build(Object... data) {
        WidgetDataResp widgetDataResp = new WidgetDataResp();
        widgetDataResp.ds = DS_MAIN;
        widgetDataResp.flushTime = new Date();
        JsonNode json = JsonHelper.toJson("{}");
        for (Object d : data) {
            ((ObjectNode) json).setAll((ObjectNode) JsonHelper.toJson(d));
        }
        widgetDataResp.data = json;
        return widgetDataResp;
    }

    public String getDs() {
        return ds;
    }

    public void setDs(String ds) {
        this.ds = ds;
    }

    public Date getFlushTime() {
        return flushTime;
    }

    public void setFlushTime(Date flushTime) {
        this.flushTime = flushTime;
    }

    public JsonNode getData() {
        return data;
    }

    public void setData(JsonNode data) {
        this.data = data;
    }
}
